package com.mvp.mvpdemo.mvp.presenter

import android.content.Context
import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.mvp.ktx.isNetworkAvailable
import com.mvp.mvpdemo.base.BasePresenter
import com.mvp.mvpdemo.bean.BAnnerBean
import com.mvp.mvpdemo.bean.LoginBean
import com.mvp.mvpdemo.bean.WXBean
import com.mvp.mvpdemo.mvp.contract.HomeContract
import com.mvp.mvpdemo.mvp.model.HomeModel
import com.retrofit.call_adapter.livedata_call_adapter.ApiEmptyResponse
import com.retrofit.call_adapter.livedata_call_adapter.ApiErrorResponse
import com.retrofit.call_adapter.livedata_call_adapter.ApiSuccessResponse
import com.retrofit.retrofitutil.RetrofitCallback
import retrofit2.Call

class HomePresenter(val context: Context) : BasePresenter<HomeContract.Model, HomeContract.View>(),
    HomeContract.Presenter {
    override fun createModel(): HomeContract.Model = HomeModel(context)

    override fun login(phone: String, pwd: String) {
        if (getView() != null) {
            if (context.isNetworkAvailable()) {
                showLoading()
                val call: Call<LoginBean>? = mModel?.login(phone, pwd)
                call?.enqueue(object : RetrofitCallback<LoginBean?>() {
                    override fun onSuccess(model: LoginBean?) {

                        if (model != null) {
                            getView()?.susscess(model)
                            Log.e("main", "res:${model}")
                        }
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        getView()?.error()
                    }

                    override fun onThrowable(t: Throwable) {

                    }

                    override fun onFinish() {
                        hideLoading()
                    }
                })
            } else {
                getView()?.showShortToast("网络连接错误，请检查网络")
            }
        }
    }


    override fun login2(phone: String, pwd: String) {
        if (getView() != null) {
            if (context.isNetworkAvailable()) {
                showLoading()

                val call: Call<LoginBean>? = mModel?.login2(phone, pwd)
                call?.enqueue(object : RetrofitCallback<LoginBean?>() {
                    override fun onSuccess(model: LoginBean?) {
                        model?.let {
                            getView()?.susscess(it)
                            Log.e("main", "res:${it}")
                        }
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        getView()?.error()
                    }

                    override fun onThrowable(t: Throwable) {
                        getView()?.showShortToast("网络响应失败,请检查网络")
                    }

                    override fun onFinish() {
                        hideLoading()
                    }
                })
            } else {
                getView()?.showShortToast("网络连接错误，请检查网络")
            }

        }
    }

    override fun getBanner() {

        if (getView() != null) {
            showLoading()

            val bannerList = mModel?.getBanner()
            bannerList?.observe(context as LifecycleOwner, Observer {
                hideLoading()
                if (it is ApiSuccessResponse<BAnnerBean>) {
                    getView()?.getBanner(it.body)
                    Log.e("main", "res:${it}")
                }
                if (it is ApiErrorResponse<BAnnerBean> || it is ApiEmptyResponse<BAnnerBean>) {
                    getView()?.error()
                }
            })


        }

    }

    override fun getWX() {
        showLoading()
        val data = mModel?.getWX()
        data?.observe(context as LifecycleOwner, Observer {
            hideLoading()
            if (it is ApiSuccessResponse<WXBean>) {
                getView()?.getWX(it.body)
                Log.e("main", "res:${it}")
            }
            if (it is ApiErrorResponse<WXBean> || it is ApiEmptyResponse<WXBean>) {
                getView()?.error()
            }
        })

    }

}
